package com.tistory.wonsama.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class WFile {


	public static int splitFile(String filePath, int fileSize)
	{
		BufferedReader br = null;	//file read
		FileInputStream fis = null;
		InputStreamReader isr = null;

		FileOutputStream fos = null;
		OutputStreamWriter osw = null;

		return -1;
	}

	/**
	 * CHECK : UTF_8 ENCODING 
	 * READ FILE
	 * @param filePath FILE PATH
	 * @return READ STRING LIST
	 * @since 2012.01.06
	 */
	public static List<String> readFile(String filePath)
	{
		List<String> list = new  ArrayList<String>();

		try{
			FileReader fr = new FileReader( filePath );
			BufferedReader br = new BufferedReader( fr );

			String tmpRead = "";

			while( ( tmpRead = br.readLine() )!=null ){
				list.add( tmpRead );
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	/**
	 * 입력받은 MESSAGE를 FILE에 기록한다 
	 * @param filePath 파일 경로 
	 * @param message 작성할 메시지 
	 * @throws Exception 오류 
	 * @since 2012.01.06
	 */
	public static void writeFile(String filePath, String message) throws Exception
	{
		//MKDIR
		mkDir(filePath);

		//INIT
		File file = new File( filePath ); 
		InputStream is = new ByteArrayInputStream( message.getBytes() );
		OutputStream os = new FileOutputStream(file);

		//WRITE
		int read;
		byte readByte[] = new byte[1024];
		while ((read = is.read(readByte, 0, 1024)) != -1) {  
			os.write(readByte, 0, read);  
		} 

		//CLOSE
		if(os!=null) os.close();
		if(is!=null) is.close();
	}

	/**
	 * 폴더를 생성한다
	 * @param fullPath 파일경로를 포함한 전체 경로
	 * @return 폴더 생성유무
	 * @since 2012.01.06
	 */
	public static boolean mkDir(String fullPath)
	{
		File file = new File( getPath( fullPath ) );
		return file.mkdir();		
	}


	/**
	 * CHECK : UTF_8 ENCODING 
	 * READ FILE : HAS INCLUDE WORDS IN LINE 
	 * @param filePath FILE PATH
	 * @param includeWord INCLUE WORD
	 * @return READ STRING LIST
	 * @since 2012.01.06
	 */
	public static List<String> readFile(String filePath, List<String> includeWord)
	{
		List<String> list = new  ArrayList<String>();

		try{
			FileReader fr = new FileReader( filePath );
			BufferedReader br = new BufferedReader( fr );

			String tmpRead = "";

			while( ( tmpRead = br.readLine() )!=null ){
				if( includeWord!=null && WString.hasContain( tmpRead, includeWord ) ){
					list.add( tmpRead );
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}


	/**
	 * 전체 경로에서 파일 경로만 얻어온다
	 * @param fullPath 전체 경로
	 * @return 파일경로
	 * @since 2012.01.06
	 */
	public static String getPath(String fullPath)
	{
		int index = -1;

		//WINDOWS
		index = fullPath.lastIndexOf("\\");
		if( index >= 0 ){
			return fullPath.substring( 0 , index );
		}

		//UNIX
		index = fullPath.lastIndexOf("/");
		if( index >= 0 ){
			return fullPath.substring( 0 , index );
		}

		return fullPath;
	}
	
	/**
	 * 전체 경로에서 파일 명을 얻어온다
	 * @param fullPath 전체 경로
	 * @return 파일경로
	 * @since 2012.01.06
	 */
	public static String getName(String fullPath)
	{
		int index = -1;

		//WINDOWS
		index = fullPath.lastIndexOf("\\");
		if( index >= 0 ){
			return fullPath.substring( index );
		}

		//UNIX
		index = fullPath.lastIndexOf("/");
		if( index >= 0 ){
			return fullPath.substring( index );
		}

		return fullPath;
	}
}
